#include "npc.hpp"
#include <string>
#include <iostream>
#include <allegro.h>

cNPC::cNPC(float x, float y, float a, const char * filename, cItemResources * itemRes)
{
    pos = new sPoint(x, y);
    angle = a;
    eq = new cEQ(0, 0, 5, 5, itemRes);
    frame = 0;

    std::string fName = "GFX\\NPC\\";
    fName += filename;
    img = load_bmp(fName.c_str(), default_palette);
    tmp = create_bitmap(TILE_SIZE, TILE_SIZE);
    clear_to_color(tmp, 0xFF00FF);

    dialog = new cDialog("", "Witaj wedrowcze!");
}

cNPC::~cNPC()
{
    destroy_bitmap(img);
    destroy_bitmap(tmp);
    delete pos;
    delete eq;
}

void cNPC::draw(BITMAP * bufor, sPoint * show, sPoint * shift, bool ** fog, bool fogOn)
{
    if (pos->x < show->x || pos->x > show->x + bufor->w || pos->y < show->y || pos->y > show->y + bufor->h) return;
    if (fogOn) if (!fog[(int)pos->x/TILE_SIZE][(int)pos->y/TILE_SIZE]) return;

    int action = 0;

    blit(img, tmp, action * TILE_SIZE, (int)(frame) * TILE_SIZE, 0, 0, TILE_SIZE, TILE_SIZE);

    int X = pos->x - show->x;
    int Y = pos->y - show->y;
    if (eq->isDrawing) eq->draw(bufor, -1, -1);

    if (action == 0) angle = -countAngle(mouse_x - shift->x - X, mouse_y - shift->y - Y);

    rotate_sprite(bufor, tmp, X - TILE_SIZE/2 + shift->x, Y - TILE_SIZE/2 + shift->y, ftofix(rtoa(angle) * 255 / 360));
}

void cNPC::addSubDialog(cDialog * dial, std::string c, std::string a)
{
    dial->addSubDialog(c, a);
}
